package net.bithappens.hnqe.mapper.graphxml;

import java.io.File;

import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.apache.log4j.Logger;

public class XMLGraphValidator {

	private static Logger log = Logger.getLogger(XMLGraphValidator.class);
	
	public static boolean validate(String xmlFilePath, String xmlSchemaPath){
		return validate(new File(xmlFilePath), new File(xmlSchemaPath));
	}
	
	public static boolean validate(File xmlFile, File xmlSchemaFile){
		
		try{
			
			SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI );
			
			Schema schema = factory.newSchema(xmlSchemaFile);

			Validator validator = schema.newValidator();
			
			Source source = new StreamSource(xmlFile);

			validator.validate(source);
			
			return true;
			
		} catch (Exception e) {
			log.error("Schemadefinition in file \""+xmlFile.getName()+"\" is not valid: " + e.getMessage());
		}
		
		return false;
	}

}
